package com.zbzly.reactive.reactiverbac.service;

import com.zbzly.reactive.reactiverbac.mapper.ReactiveUserRepository;
import jakarta.annotation.Resource;
import org.springframework.security.authentication.ReactiveAuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
public class ReactiveUserManagerServiceImpl implements ReactiveAuthenticationManager {
    @Resource
    private ReactiveUserRepository reactiveUserRepository;

    @Override
    public Mono<Authentication> authenticate(Authentication authentication) {
       return reactiveUserRepository.findByUsername(authentication.getPrincipal().toString())
               .switchIfEmpty(Mono.error(new UsernameNotFoundException("Username not found")))
               .map((user)-> authentication);
    }
}
